﻿using System;
using System.Data.SqlClient;
using System.Runtime.Serialization;

namespace NPMonitor.Utility
{
    [DataContract]
    public class HttpActions
    {
        [DataMember]
        public int ActionId { get; set; }

        public Guid HistoryUniqueId { get; set; }
        /// <summary>
        /// The time when the loading of this page was started, as an offset in seconds from the time that the log was started.  
        /// </summary>
        public double Started { get; set; }
        /// <summary>
        /// Full date and time at which the loading of this page was started.
        /// </summary>
        public DateTime BeginTime { get; set; }
        /// <summary>
        /// The time in seconds from the start of the first request to the end of the last item to be completed.  
        /// </summary>
        public double Time { get;set; }
        public string Url { get; set; }
        public string Title { get; set; }
        public int SendBytes { get; set; }
        public int ReceivedBytes { get; set; }
        public int ErrorCount { get; set; }
        public int PoolId { get; set; }
        public string MachineName { get; set; }

        public string ScenarioNO { get; set; }

        public DateTime BeginTimeFormat
        {
            get
            {
                return new DateTime(BeginTime.Year, BeginTime.Month, BeginTime.Day
                     , BeginTime.Hour, (BeginTime.Minute / 5) * 5, 0);
            }
        }

        protected string InsertSql
        {
            get
            {
                return string.Format(@"INSERT INTO [HttpActions]
                                   ([HistoryUniqueId]
                                   ,[Started]
                                   ,[BeginTime]
                                   ,[BeginTimeFormat]
                                   ,[Time]
                                   ,[Url]
                                   ,[Title]
                                   ,[SendBytes]
                                   ,[ReceivedBytes]
                                   ,[ErrorCount]
                                   ,[PoolId]
                                   ,[MachineName]
                                   ,[ScenarioNO])
                                   VALUES(@HistoryUniqueId,@Started,@BeginTime,@BeginTimeFormat,@Time,@Url,@Title,@SendBytes,@ReceivedBytes,@ErrorCount,@PoolId,@MachineName,@ScenarioNO);
                                    Select @@Identity;");
            }
        }

        /// <summary>
        /// Return Insert Id
        /// </summary>
        /// <returns></returns>
        public int Insert()
        {
            SqlParameter[] sqlParameters = {
                                               new SqlParameter("@HistoryUniqueId",HistoryUniqueId),
                                               new SqlParameter("@Started", Started),
                                               new SqlParameter("@BeginTime", BeginTime),
                                               new SqlParameter("@BeginTimeFormat", BeginTimeFormat),
                                               new SqlParameter("@Time", Time),
                                               new SqlParameter("@Url", Url),
                                               new SqlParameter("@Title",Title),
                                               new SqlParameter("@SendBytes", SendBytes),
                                               new SqlParameter("@ReceivedBytes", ReceivedBytes),
                                               new SqlParameter("@ErrorCount", ErrorCount),
                                               new SqlParameter("@PoolId", PoolId),
                                               new SqlParameter("@MachineName",MachineName),
                                               new SqlParameter("@ScenarioNO",ScenarioNO)
                                           };
            string id = SqlHelper.ExecuteScalar(InsertSql, sqlParameters);
            int actionId;
            if (int.TryParse(id, out actionId))
            {
                ActionId = actionId;
            }
            return actionId;
        }
    }

}
